package com.ghovos.usermangerpgdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ghovos.usermangerpgdemo.entity.DepartmentEntity;
import com.ghovos.usermangerpgdemo.entity.UserEntity;
import com.ghovos.usermangerpgdemo.mapper.DepartmentMapper;
import com.ghovos.usermangerpgdemo.mapper.UserMapper;
import com.ghovos.usermangerpgdemo.service.DepartmentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author EdgarLiu(刘凌峰) <br/>
 * date: 2022/3/18 <br/>
 * time: 上午11:07 <br/>
 * email: EdgarLlf@foxmail.com
 */
@Service
@Slf4j
@AllArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, DepartmentEntity> implements DepartmentService {
    public final DepartmentMapper departmentMapper;
    public final UserMapper userMapper;

    @Override
    public List<DepartmentEntity> allDepartmentListExt() {
        List<DepartmentEntity> departmentEntities = departmentMapper.selectList(null);
        log.info("找到的department个数: {}", departmentEntities.size());
        departmentEntities.forEach(this::getDepartmentExtList);
        return departmentEntities;
    }

    @Override
    public DepartmentEntity getDepartExtById(Long id) {
        DepartmentEntity departmentEntity = departmentMapper.selectById(id);

        List<UserEntity> userEntities = userMapper.selectList(
                Wrappers.<UserEntity>lambdaQuery()
                        .eq(UserEntity::getDepartmentId, departmentEntity.getId())
        );
        departmentEntity.setUserEntityList(userEntities);
        return departmentEntity;
    }


    @Override
    public boolean deleteByIdWithUser(Long id) {

        LambdaUpdateWrapper<DepartmentEntity> departmentQueryWrapper = Wrappers.<DepartmentEntity>lambdaUpdate()
                .eq(DepartmentEntity::getId, id)
                .set(DepartmentEntity::getUpdateTime, new Date());
        departmentMapper.update(null, departmentQueryWrapper);

        LambdaUpdateWrapper<UserEntity> userQueryWrapper = Wrappers.<UserEntity>lambdaUpdate()
                .eq(UserEntity::getDepartmentId, id)
                .set(UserEntity::getDepartmentId, null);

        boolean successDelete = departmentMapper.deleteById(id) > 0;
        boolean successUpdate= userMapper.update(new UserEntity(), userQueryWrapper) > 0;
        return successDelete && successUpdate;
    }

    @Override
    public boolean addUserByIds(Long departmentId, List<Long> ids) {
        LambdaUpdateWrapper<UserEntity> userUpdateWrapper = Wrappers.<UserEntity>lambdaUpdate()
                .in(UserEntity::getId, ids)
                .set(UserEntity::getDepartmentId, departmentId);
        int update = userMapper.update(new UserEntity(), userUpdateWrapper);
        return update > 0;
    }

    @Override
    public boolean deleteUserByIds(Long departmentId, List<Long> ids) {
        LambdaUpdateWrapper<UserEntity> userUpdateWrapper = Wrappers.<UserEntity>lambdaUpdate()
                .eq(UserEntity::getDepartmentId, departmentId)
                .in(UserEntity::getId, ids)
                .set(UserEntity::getDepartmentId, null);
        int update = userMapper.update(new UserEntity(), userUpdateWrapper);
        return update > 0;
    }

    @Override
    public DepartmentEntity getExtById(Long id) {
        DepartmentEntity departmentEntity = departmentMapper.selectById(id);
        getDepartmentExtList(departmentEntity);
        return departmentEntity;
    }

    /**
     * 获取department 的额外连接表（用户）信息
     * @param departmentEntity 部门表
     */
    private void getDepartmentExtList(DepartmentEntity departmentEntity){
        List<UserEntity> userEntities = userMapper.selectList(
                Wrappers.<UserEntity>lambdaQuery()
                        .eq(UserEntity::getDepartmentId, departmentEntity.getId())
        );
        departmentEntity.setUserEntityList(userEntities);
    }

}
